<?php

namespace app\admin\model\shopro\activity;

use think\Model;
use traits\model\SoftDelete;
use addons\shopro\model\commission\Order as CommissionOrder;
use addons\shopro\model\commission\Agent as AgentModel;
class ActivityOrder extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'shopro_activity_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'createtime_text',
        'paytime_text'
    ];
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }
   
    public function getPaytimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['paytime']) ? $data['paytime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i", $value) : $value;
    }
    
    public function activity()
    {
        return $this->belongsTo('\app\admin\model\shopro\activity\Activity', 'activity_id','id');
    }
     public function user()
    {
        return $this->belongsTo('\app\admin\model\shopro\user\User', 'user_id', 'id');
    }
      public function agent()
    {
        return $this->belongsTo(\app\admin\model\shopro\commission\Agent::class, 'user_id','user_id');
    }
     /**
     * 订单支付成功
     *
     * @param [type] $order
     * @param [type] $notify
     * @return void
     */
    public function paymentProcess($order, $notify)
    {
        // file_put_contents("1.txt",json_encode($notify));
        // file_put_contents("2.txt",json_encode($order));
        try{
            $order->pay_status = 1;
            $order->paytime = time();
            $order->transaction_id = $notify['transaction_id'];
            $order->payment_json = $notify['payment_json'];
            $order->pay_type = $notify['pay_type'];
            $order->save();
        } catch (Exception $e) {
            file_put_contents("3.txt",$e->getMessage());
        }
        return $order;
    }
    public static  function getInfo($user_id,$activity,$childs = ''){
        $activity_order = self::where("user_id",$user_id)->where("activity_id",$activity['id'])->where("pay_status",1)->find();
        $result = [];
        if($activity_order){
            $result['sign_status'] = 'signed';
            $result['sign_status_text'] = '已报名';
            // 统计订单数量
            $result['complete_order_count'] = 0;
            $result['percent'] = 0;
            if($activity['status'] != 'nostart'){
                if($activity['sale_type'] == 'team'){
                    if(!$childs){
                        $agentModel = new AgentModel();
                        $childs = $agentModel->getChild($user_id);
                    }
                    $child = $childs['child'];
                    $child[] = $user_id;
                } else {
                    $child = [$user_id];
                }
                if($activity_order['paytime'] > $activity['starttime']){
                    $start = $activity_order['paytime'];
                } else {
                    $start = $activity['starttime'];
                }
                $end = $activity['endtime'];
                $result['complete_order_count'] = CommissionOrder::whereIn('agent_id', $child)->where('commission_order_status', CommissionOrder::COMMISSION_ORDER_STATUS_YES)->whereBetween('createtime', [$start, $end])->count();
                $result['percent'] = $result['complete_order_count'] ? bcdiv($result['complete_order_count'],$activity['order_count'],2) * 100: 0;
                if($activity['status'] == 'ended'){
                    $can_receive_time = $end + $activity['reward_day'] * 24 * 60 * 60;
                    $result['receive_status'] = $activity_order['status'];
                    if($activity_order['status'] == 2){
                        $result['receive_status_text'] = "已领取".$activity_order['reward']."元";
                    } else if($activity_order['status'] == 1){
                        $result['receive_status_text'] = "审核中";
                    } else if(time() < $can_receive_time){
                        $result['receive_status'] = -1;
                        $result['receive_status_text'] = date("m-d H:i",$can_receive_time)."后可领取";
                    } else {
                        $result['receive_status_text'] = "待领取";
                    }
                }
            }
        }
        return $result;
    }
}
